package com.ruoyi.project.module.ossFileCache.service;

import java.util.List;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ListObjectsRequest;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
import com.google.common.collect.Lists;
import com.ruoyi.project.config.AliyunOSSCoreProperties;
import com.ruoyi.project.module.osssearch.model.OssSearch;
import org.shoukaiseki.apiview.common.util.ByteUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.module.ossFileCache.mapper.OssFileCacheMapper;
import com.ruoyi.project.module.ossFileCache.domain.OssFileCache;
import com.ruoyi.project.module.ossFileCache.service.IOssFileCacheService;
import com.ruoyi.common.support.Convert;
import javax.annotation.Resource;

/**
 * OSS 缓存 服务层实现
 * 
 * @author ruoyi
 * @date 2019-05-06
 */
@Service
public class OssFileCacheServiceImpl implements IOssFileCacheService 
{
	@Resource
	private OssFileCacheMapper ossFileCacheMapper;

	@Resource
	OSSClient ossClient;

	@Resource
	AliyunOSSCoreProperties aliyunOSSCoreProperties;


	/**
     * 查询OSS 缓存列表
     * 
     * @param ossFileCache OSS 缓存信息
     * @return OSS 缓存集合
     */
	@Override
	public List<OssFileCache> selectOssFileCacheList(OssFileCache ossFileCache)
	{
	    return ossFileCacheMapper.selectOssFileCacheList(ossFileCache);
	}
	
    /**
     * 新增OSS 缓存
     * 
     * @param ossFileCache OSS 缓存信息
     * @return 结果
     */
	@Override
	public int insertOssFileCache(OssFileCache ossFileCache)
	{
	    return ossFileCacheMapper.insertOssFileCache(ossFileCache);
	}
	

	/**
     * 删除OSS 缓存对象
     * 
     * @return 结果
     */
	@Override
	public int deleteOssFileCache()
	{
		return ossFileCacheMapper.clearOssFileCache();
	}

	@Override
	public int createCache() {
		List<OssFileCache> list = Lists.newArrayList();
		//每次获取的条数
		int maxkeys=1000;

		String prefix="image";

		String bucketName="orighere";

		ObjectListing objectListing = null;

		String nextMarker=null;

		do{
			// MaxKey默认值为100，最大值为1000。
			ListObjectsRequest request = new ListObjectsRequest(bucketName).withPrefix(prefix)
					.withMaxKeys(maxkeys);

			if(nextMarker!=null){
				request.setMarker(nextMarker);
			}

			objectListing = ossClient.listObjects(request);
			List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
			list.clear();
			for (OSSObjectSummary s : sums) {
				long size=s.getSize();
				//超过最小限制,则忽略
				OssFileCache ossSearch=new OssFileCache();
				ossSearch.setFileName(s.getKey());
				ossSearch.setFileSize(size);
//				ossSearch.setFileSizeStr(ByteUtils.byteFormat(size));
				String url = "https://" + bucketName + "." + aliyunOSSCoreProperties.getEndpoint() + "/" + s.getKey();
				ossSearch.setUrl(url);
				ossSearch.setLastModified(s.getLastModified());
				ossSearch.setBucketName(bucketName);
				list.add(ossSearch);
			}

			nextMarker = objectListing.getNextMarker();
			ossFileCacheMapper.insertBatch(list);
		}while (objectListing.isTruncated());


		return 1;
	}
}
